package com.rrqs.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rrqs.activity.common.Common;
import com.rrqs.activity.common.Const;
import com.rrqs.activity.data.MytjData;
import com.rrqs.frame.JsonProcessHelper;
import com.zadtek.xmqishi.R;

/**
 * 我的统计
 * 
 * @author kuyukeji
 * 
 */
public class MytjChildFragment extends Fragment {

	private Context context;
	private LayoutInflater inflater;
	private Activity myActivity;
	private Resources res;

	private MytjData mytjdata;

	private TextView pinfscore, paimin, totaldan, paiminzd; // 配送服务评分,排名,总单量,单量排名
	private TextView pszsl, paiminps, chaos, chaosd; // 配送准时率,配送准时排名,超时单量
	private ImageView toright;
	
	private LinearLayout scorephlay;	//配送评分排名
	
	private TextView scorename, allname, ratename, c4name, c6name; 

	private SharedPreferences sp;
	private String time = ""; // 时间(昨天,今天,本月,上月),默认今天
	private String status = "";
	private TextView manyilv,manyipaiming,zonghepingfen,zonghepingfenpaiming,zhunshisongdalv,zhunshisongdalvpaiming,pingjunshichang,pingjunshichangpaiming
			,songhuojuli,songhuojulipaiming,peisongdanliang,peisongdanliangpaiming,yibanchaoshi,yibanchaoshipaiming,yanzhongchaoshi,yanzhongchaoshipaiming;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		myActivity = getActivity();
	}

	// 设置时间
	public void setTime(String status) {
		if(status.length()>2){
			this.time = status;
		}else {
			this.status = status;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_mytj_child, null);
		context = view.getContext();
		this.inflater = inflater;
		myActivity = getActivity();
		res = getResources();
//
//		scorename = (TextView) view.findViewById(R.id.scorename);
//		allname = (TextView) view.findViewById(R.id.allname);
//		ratename = (TextView) view.findViewById(R.id.ratename);
//		c4name = (TextView) view.findViewById(R.id.c4name);
//		c6name = (TextView) view.findViewById(R.id.c6name);
//
//		pinfscore = (TextView) view.findViewById(R.id.pinfscore);
//		paimin = (TextView) view.findViewById(R.id.paimin);
//		totaldan = (TextView) view.findViewById(R.id.totaldan);
//		paiminzd = (TextView) view.findViewById(R.id.paiminzd);
//		pszsl = (TextView) view.findViewById(R.id.pszsl);
//		paiminps = (TextView) view.findViewById(R.id.paiminps);
//		chaos = (TextView) view.findViewById(R.id.chaos);
//		chaosd = (TextView) view.findViewById(R.id.chaosd);
//		toright = (ImageView) view.findViewById(R.id.toright);
//		scorephlay = (LinearLayout) view.findViewById(R.id.scorephlay);
//		scorephlay.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				Intent paih = new Intent(getActivity(), PaihFragment.class);
//				paih.putExtra("tostate", "1");
//				paih.putExtra("totime", time);
//				startActivity(paih);
//			}
//		});
		manyilv = (TextView) view.findViewById(R.id.manyilv);
		manyipaiming = (TextView) view.findViewById(R.id.manyipaiming);
		zonghepingfen = (TextView) view.findViewById(R.id.zonghepingfen);
		zonghepingfenpaiming = (TextView) view.findViewById(R.id.shangjiapingfenpaiming);
		zhunshisongdalv = (TextView) view.findViewById(R.id.zhunshisongdalv);
		zhunshisongdalvpaiming = (TextView) view.findViewById(R.id.zhunshisongdalvpaiming);
		pingjunshichang = (TextView) view.findViewById(R.id.pingjunshichang);
		pingjunshichangpaiming = (TextView) view.findViewById(R.id.pingjunshichangpaiming);
		songhuojuli = (TextView) view.findViewById(R.id.songhuojuli);
		songhuojulipaiming = (TextView) view.findViewById(R.id.songhuojulipaiming);
		peisongdanliang = (TextView) view.findViewById(R.id.peisongdanliang);
		peisongdanliangpaiming = (TextView) view.findViewById(R.id.peisongdanliangpaiming);
		yibanchaoshi = (TextView) view.findViewById(R.id.yibanchaoshi);
		yibanchaoshipaiming = (TextView) view.findViewById(R.id.yibanchaoshipaiming);
		yanzhongchaoshi = (TextView) view.findViewById(R.id.yanzhongchaoshi);
		yanzhongchaoshipaiming = (TextView) view.findViewById(R.id.yanzhongchaoshipaiming);
		getinitArray();

		return view;
	}

	// 获取初始化数据
	public void getinitArray() {
		initArrays(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (!"emp".equals(msg.obj.toString())) {
					mytjdata = (MytjData) msg.obj;
					if (mytjdata != null && mytjdata.getStatus().equals("1")) {
						manyilv.setText(mytjdata.getManyiarr().getManyi()+"");
						manyipaiming.setText(mytjdata.getManyiarr().getManyipaiming()+"");
						zonghepingfen.setText(mytjdata.getZonghearr().getZonghe()+"");
						zonghepingfenpaiming.setText(mytjdata.getZonghearr().getZonghepaiming()+"");
						zhunshisongdalv.setText(mytjdata.getZhunshiarr().getZhunshi()+"");
						zhunshisongdalvpaiming.setText(mytjdata.getZhunshiarr().getZhunshipaiming()+"");
						pingjunshichang.setText(mytjdata.getShichangarr().getShichang()+"");
						pingjunshichangpaiming.setText(mytjdata.getShichangarr().getShichangaiming()+"");
						songhuojuli.setText(mytjdata.getJuliarr().getJuli()+"");
						songhuojulipaiming.setText(mytjdata.getJuliarr().getJulipaiming()+"");
						peisongdanliang.setText(mytjdata.getDanliangarr().getDanliang()+"");
						peisongdanliangpaiming.setText(mytjdata.getDanliangarr().getDanliangpaiming()+"");
						yibanchaoshi.setText(mytjdata.getYibanarr().getYiban()+"");
						yibanchaoshipaiming.setText(mytjdata.getYibanarr().getYibanpaiming()+"");
						yanzhongchaoshi.setText(mytjdata.getYanzhongarr().getYanzhong()+"");
						yanzhongchaoshipaiming.setText(mytjdata.getYanzhongarr().getYanzhongpaiming()+"");
//						pinfscore.setText(mytjdata.getZonghearr().getZonghe());
//						paimin.setText(mytjdata.getZonghearr().getZonghepaiming());
//						String scorenote = mytjdata.getScorenote();
//						if(scorenote!=null && !scorenote.equals("")) {
//							scorename.setText(scorenote);
//						}
//						totaldan.setText(mytjdata.getAlltotal());
//						paiminzd.setText(mytjdata.getAllscortorder());
//						allname.setText(mytjdata.getAllnote());
//						pszsl.setText(mytjdata.getRatetotal());
//						paiminps.setText(mytjdata.getRatesortorder());
//						ratename.setText(mytjdata.getRatenote());
//						String c4total = mytjdata.getC4total();
//						if (c4total != null && !c4total.equals("")
//								&& Integer.parseInt(c4total) > 0) {
//							chaos.setTextColor(res.getColor(R.color.red));
//						} else {
//							chaos.setTextColor(res.getColor(R.color.black));
//						}
//						chaos.setText(c4total);
//						c4name.setText(mytjdata.getC4note());
//						String c6total = mytjdata.getC6total();
//						if (c6total != null && !c6total.equals("")
//								&& Integer.parseInt(c6total) > 0) {
//							chaosd.setTextColor(res.getColor(R.color.red));
//						} else {
//							chaosd.setTextColor(res.getColor(R.color.black));
//						}
//						chaosd.setText(c6total);
//						c6name.setText(mytjdata.getC6note());
					} else {
						Common.DisplayToast(context, mytjdata.getFail(), 1);
					}

				} else {
					Common.DisplayToast(context, "通信失败,请检查网络!", 1);
				}
			}
		});
	}

	// 线程加载数据
	private void initArrays(final Handler handler) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					// 从缓存中获取用户信息
					sp = myActivity.getSharedPreferences(Const.PREF_NAME,
							Context.MODE_PRIVATE);
					String uid = sp.getString("uid", "");
					MytjData resultdata = JsonProcessHelper
							.jsonProcess_getoneqstotal("get_one_qishi_total",
									uid,time, status);
					if (resultdata != null) {
						handler.obtainMessage(0, resultdata).sendToTarget();
					} else {
						handler.obtainMessage(0, "emp").sendToTarget();
					}

				} catch (Exception e) {
					Thread.interrupted();
					e.printStackTrace();
				}
			}
		}).start();
	}

	public void refresh() {
		try{
			getinitArray();
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
